package com.tencent.wxcloudrun.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencent.wxcloudrun.dto.WechatMsgDTO;
import com.tencent.wxcloudrun.WechatMsgRespManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author zhe.wang@weimob.com
 * @date 2023/4/3 下午5:28
 */
@RestController
@RequestMapping("/wechat/msg")
@Slf4j
public class WechatMsgController {

    @Autowired
    WechatMsgRespManager wechatMsgRespManager;

    @PostMapping("")
    public String handleMsg(@RequestBody Object msg) {

        String msgStr = JSON.toJSONString(msg);
        log.info("handle wechart msg :{}", msgStr);
        WechatMsgDTO msgDTO = JSONObject.parseObject(msgStr, WechatMsgDTO.class);
        WechatMsgDTO msgRespDTO = wechatMsgRespManager.response(msgDTO);

        String respMessage = wechatMsgRespManager.toJSON(msgRespDTO).replaceAll("\n", "");
        log.info("handle wechart msg resp :{}", respMessage);
        return respMessage;

    }

}
